var markerArray = [];
var reloadMaps = [];
var vResultsFrom = 0;
var numOfResults = 10;
var jsonInfo = [];
var infowindow;
var test;

//when the page has loaded execute loadList()
$(document).ready(function() {

	placeMarkers();

});

function placeMarkers() {

	var icon;
	// google maps stuff

	//default position for map
	var latlng = new google.maps.LatLng(33.783409, -117.884674);
	//defualt options for map
	var myOptions = {
		zoom : 11,
		center : latlng,
		mapTypeId : google.maps.MapTypeId.HYBRID
	//type of map HYBRID/ROADMAP/SATELLITE/TERRAIN
	};
	var map = new google.maps.Map(document.getElementById('map_canvas'),
			myOptions);
	
	 infowindow = new google.maps.InfoWindow({
		content : "temp"
	});


	$.getJSON('tab_function.php', function(output) {
		$.each(output, function(i) {
			var lat = output[i]['lat'];
			var lng = output[i]['lng'];
			var id = output[i]['id'];
			var name = output[i]['name'];
			var category = output[i]['category'];

			var latlng = new google.maps.LatLng(lat, lng);

			switch (category) {
			case "other":
				icon = new google.maps.MarkerImage('images/vineyard-2.png',
						new google.maps.Size(32, 37), new google.maps.Point(0,
								0), new google.maps.Point(16, 37));
				break;
			case "farm":
				icon = new google.maps.MarkerImage('images/yellow.png',
						new google.maps.Size(32, 37), new google.maps.Point(0,
								0), new google.maps.Point(16, 37));
				break;
			case "csa":
				icon = new google.maps.MarkerImage('images/blue.png',
						new google.maps.Size(32, 37), new google.maps.Point(0,
								0), new google.maps.Point(16, 37));
				break;
			case "farmers_market":
				icon = new google.maps.MarkerImage('images/purple.png',
						new google.maps.Size(32, 37), new google.maps.Point(0,
								0), new google.maps.Point(16, 37));
				break;
			case "orchards":
				icon = new google.maps.MarkerImage('images/white.png',
						new google.maps.Size(32, 37), new google.maps.Point(0,
								0), new google.maps.Point(16, 37));
				break;
			case "community_garden":
				icon = new google.maps.MarkerImage('images/turq.png',
						new google.maps.Size(32, 37), new google.maps.Point(0,
								0), new google.maps.Point(16, 37));
				break;
			}

			var marker = new google.maps.Marker({
				position : latlng,
				map : map,
				icon : icon,
				title : name
			});

			marker.html = $(output[i]['infobox']).html();
			marker.category = category;

			google.maps.event.addListener(marker, 'click', function() {
				infowindow.setContent(this.html);
				infowindow.open(map, this);
			});
			markerArray.push(marker);
		});
	});
}
function openWindow(i) {
	google.maps.event.trigger(markerArray[i], 'click');
}

function update(numOfMarkers) {

}



function reserve_pickup(vId) {
	$.post("reservePickup.php", {
		id : vId
	}, function() {
		window.location.reload();
	});
}

function toggleVisible(theCategory) {
	infowindow.close();
	var notCheck = $('input[type=checkbox]').not(':checked').length;
	
	if ((theCategory == 'all') && ($('input[id = all]').is(':checked'))){
		$('input[id = farm]').attr('checked',true);
		$('input[id = farmersMarket]').attr('checked',true);
		$('input[id = communityGarden]').attr('checked',true);
		$('input[id = orchards]').attr('checked',true);
		$('input[id = csa]').attr('checked',true);
		$('input[id = other]').attr('checked',true);
	}else if ((theCategory == 'all') && !$('input[id = all]').is(':checked')){
		$('input[id = farm]').attr('checked',false);
		$('input[id = farmersMarket]').attr('checked',false);
		$('input[id = communityGarden]').attr('checked',false);
		$('input[id = orchards]').attr('checked',false);
		$('input[id = csa]').attr('checked',false);
		$('input[id = other]').attr('checked',false);
	}else{
		if(	$('input[id = farm]').is(':checked')&& 
		$('input[id = farmersMarket]').is(':checked')&&
		$('input[id = communityGarden]').is(':checked')&&
		$('input[id = orchards]').is(':checked')&&
		$('input[id = csa]').is(':checked')&&
		$('input[id = other]').is(':checked')){
			$('input[id = all]').attr('checked',true);
		}else
			$('input[id = all]').attr('checked',false);
	}

	for ( var i = 0; i < markerArray.length; i++) {
		var marker = markerArray[i];

		if (theCategory == 'all'){
				
			if($('input[id = all]').is(':checked'))
				marker.setVisible(true);
			else
				marker.setVisible(false);
		}
		else if (markerArray[i].category == theCategory){
			marker.setVisible(!(marker.getVisible()));
		}
	}

}